﻿using System;
using Relocation.Base;
using Relocation.Base.UI;
using Relocation.Com;
using Relocation.Com.Tools;
using Relocation.Data;

namespace Relocation
{
	public partial class PwdModify : BaseForm
	{


		public PwdModify()
			: base()
		{
			Initialize();
		}

		public PwdModify(Session s)
			: base(s)
		{
			Initialize();
		}

		private void Initialize()
		{
			InitializeComponent();
			this.password.Tag = new ControlTag(null, new ValidatorInfo(true));
			this.confirmPassword.Tag = new ControlTag(null, new ValidatorInfo(true));
			this.newPassword.Tag = new ControlTag(null, new ValidatorInfo(true));
		}

		private void submit_Click(object sender, EventArgs e)
		{
			if (!this.ValidatorAllControls())
				return;
			User u = this.Session.DataModel.getUserByNameAndPassword(Session.User.name, this.password.Text);
			if (u == null)
			{
				MyMessagebox.Show("旧密码输入错误！");
				this.password.Text = "";
				this.password.Focus();
				return;
			} else if (!Validator.length(this.newPassword.Text, 6, 12))
			{
				MyMessagebox.Show("密码长度为6-12位！");
				this.newPassword.Text = "";
				this.newPassword.Focus();
				return;
			}
			if (!this.confirmPassword.Text.Equals(this.newPassword.Text))
			{
				MyMessagebox.Show("两次密码输入不一致！");
				this.confirmPassword.Text = "";
				this.confirmPassword.Focus();
				return;
			}
			u.password = this.newPassword.Text;
			if (this.Session.DataModel.Save(u) < 1)
			{
				MyMessagebox.Show("密码修改失败！");
				return;
			} else
			{
				MyMessagebox.Show("密码修改成功！");
			}
			this.Close();
		}
	}
}
